package com.nchu.web.interceptor;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.nchu.web.model.ProblemsHelper;
import com.nchu.web.model.Quiz;
import com.nchu.web.model.Teacher;
public class ApplicationStuInterceptor implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
	    HttpSession session=controller.getSession();
	    String s=(String)session.getAttribute("student_user");
	   // String str =controller.getSessionAttr("Tproblem");
	    /**application --getAttr("problem")=null
	    String pro=controller.setAttr("application",JFinal.me().getServletContext()).getAttr("problem");
		*/	  
		List<Teacher> list = Teacher.dao.belongTeacher(s);
		String teaID = list.get(0).getTNo();
		System.out.println(teaID);
		ProblemsHelper ph = ProblemsHelper.dao.findByTeacherName(teaID);
		String content ="";
		if(ph==null){
			
		}else{
			content = ph.getTProblem();
		}	
	    Quiz mq = Quiz.dao.findByTeacherName(teaID);
	    String appointStu ="";
	    String appointWay ="";
	    if(mq==null){
	    	
	    }else{
	        appointStu =mq.getStuId();
	 		appointWay =mq.getWay();
	    }	   
	    if((s!="")&&(s!=null)){		
			controller.setAttr("stu_id",s);
			controller.setAttr("myTeacher", teaID);
			controller.setAttr("application",JFinal.me().getServletContext()).setAttr("catchProblem",content);
			controller.setAttr("application",JFinal.me().getServletContext()).setAttr("appoint_stu_id",appointStu);	
			controller.setAttr("application",JFinal.me().getServletContext()).setAttr("appoint_way",appointWay);	
			inv.invoke();
	    }else{
			controller.forwardAction("/visit"); 
		}
		
	}

}
